package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "轮播图接口管理")
@RestController
@RequestMapping("/admin/indexImg")
public class IndexImageController {
    @Autowired
    private IndexImgService indexImgService;


    @ApiOperation("多条件分页查询轮播图列表")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('admin:indexImg:page')")
    public ResponseEntity<Page<IndexImg>> loadIndexImagePage(Page<IndexImg> page, IndexImg indexImg) {
        indexImgService.selectIndexImgPage(page, indexImg);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("新增轮播图")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:save')")
    public ResponseEntity<Void> save(@RequestBody IndexImg indexImg) {
        indexImgService.save(indexImg);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("根据标识查询轮播图详情")
    @GetMapping("/info/{indexImgId}")
    @PreAuthorize("hasAnyAuthority('')")
    public ResponseEntity<IndexImg> loadIndexImgInfo(@PathVariable Long indexImgId) {
        IndexImg indexImg = indexImgService.getById(indexImgId);

        return ResponseEntity.ok(indexImg);
    }

    @ApiOperation("修改轮播图")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:update')")
    public ResponseEntity<Void> update(@RequestBody IndexImg indexImg) {
        indexImgService.updateById(indexImg);
        return ResponseEntity.ok().build();
    }


    @ApiOperation("批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:delete')")
    public ResponseEntity<Void> deleteIndexImg(@RequestBody List<Long> ids) {
        indexImgService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }

    /////////////// 前台轮播图管理 ///////////////////
    @ApiOperation("查询前台轮播图")
    @GetMapping("/indexImgs")
    public ResponseEntity<List<IndexImg>> loadFrontIndexImg() {
        List<IndexImg> indexImgList = indexImgService.selectFrontIndexImg();
        return ResponseEntity.ok(indexImgList);
    }
}
